import sys
sys.path.append("..")
import Cust.leancloudapi
GetApi = Cust.leancloudapi.GetApi()
Lesson = GetApi.getLesson()
class LessonMange:
   
    def getAllLesson(self):
        query = Lesson.query
        query.limit(1000)
       
        query.include('TeacherInfo')
        res = query.find()
        Lesson_List = []
        for item in res:            
            id = item.id
            Lesson_Name = item.get('LessonName')
            Lesson_Term = item.get('Term')
            Lesson_week = item.get('Week')
            Lesson_Start = item.get('StartSection')
            Lesson_End = item.get('EndSection')
            Lesson_academy = item.get('Academy')
            Lesson_grade = item.get('Grade')
            WeekDay = item.get('WeekDay')
            ClassRoom = item.get('ClassRoom')
            TeacherM = item.get('TeacherInfo') 
            Teacher_Name = TeacherM.get('TeacherName')
            Teacher_No = TeacherM.get('TeacherNo')
            Teacher_Id = TeacherM.id
            Teacher = {'Teacher_Id':Teacher_Id,'Teacher_No':Teacher_No,'Teacher_Name':Teacher_Name}
            LessonBean = {'Lesson_Id':id,'Lesson_Name':Lesson_Name,'Teacher':Teacher,'Term':Lesson_Term,'Week':Lesson_week,'WeekDay':WeekDay,'StartSection':Lesson_Start,'EndSection':Lesson_End,'ClassRoom':ClassRoom,'grade':Lesson_grade,'academy':Lesson_academy}
            Lesson_List.append(LessonBean)
        return Lesson_List
    def insertLesson(self,lessonName,TeacherId,TermValue,Week,WeekDay,StartSection,EndSection,ClassRoom,academy,grade):
        Teacher = GetApi.getTeacher()
        Selecteacher = Teacher.create_without_data(TeacherId)
        addLesson = Lesson()
        addLesson.set('LessonName',lessonName)
        addLesson.set('TeacherInfo',Selecteacher)
        addLesson.set('Term',TermValue)
        addLesson.set('Week',Week)
        addLesson.set('StartSection',StartSection)
        addLesson.set('EndSection',EndSection)
        addLesson.set('WeekDay',WeekDay)
        addLesson.set('Grade',grade)
        addLesson.set('Academy',academy)
        addLesson.set('ClassRoom',ClassRoom)
        try:
            addLesson.save()
        except Exception as e:
            print e.message
            return 111
        else:
            return 110
    def modifyLesson(self,lesson_id,lessonName,TeacherId,TermValue,Week,WeekDay,StartSection,EndSection,ClassRoom,academy,grade):
        Teacher = GetApi.getTeacher()
        Selecteacher = Teacher.create_without_data(TeacherId)
        updatelesson = Lesson.create_without_data(lesson_id)
        updatelesson.set('LessonName',lessonName)
        updatelesson.set('TeacherInfo',Selecteacher)
        updatelesson.set('Term',TermValue)
        updatelesson.set('Week',Week)
        updatelesson.set('StartSection',StartSection)
        updatelesson.set('EndSection',EndSection)
        updatelesson.set('WeekDay',WeekDay)
        updatelesson.set('ClassRoom',ClassRoom) 
        updatelesson.set('Grade',grade)
        updatelesson.set('Academy',academy)
        try:
            updatelesson.save()
        except Exception as e:
            print e.message 
            return 131
        else:
            return 130
    def deLesson(self,lesson_id):
        
        deletelesson = Lesson.create_without_data(lesson_id)
        try:
            deletelesson.destroy()
        except Exception as e:
            print e.message
            return 121
        else:
            return 120
    def queryLesson(self,lesson_id):
        query = Lesson()     
        res = query.create_without_data(lesson_id)
        res.fetch()
        lesson_name = res.get('LessonName')
        lesson_week = res.get('Week')
        startsection = res.get('StartSection')
        endsection = res.get('EndSection')
        weekday = res.get('WeekDay') 
        classroom = res.get('ClassRoom')
        grade = res.get('Grade')
        academy = res.get('Academy')
        term = res.get('Term')
        TeacherM = res.get('TeacherInfo') 
        Teacher_Id = TeacherM.id
        lessonm = {'id':lesson_id,'teacher_id':Teacher_Id,'lesson_name':lesson_name,'term':term,'week':lesson_week,'startsection':startsection,'endsection':endsection,'classroom':classroom,'weekday':weekday,'grade':grade,'academy':academy} 
        return lessonm